package top.shiyiri.team.service;

import top.shiyiri.team.domain.Architect;
import top.shiyiri.team.domain.Designer;
import top.shiyiri.team.domain.Employee;
import top.shiyiri.team.domain.Programmer;

/**
 * @Description 关于开发团队成员的管理:添加删除等.
 */
public class TeamService {

    private static int counter = 1;//给memberId赋值
    private final int MAX_MEMBER = 5;//限制开发团队的人数
    private Programmer[] team = new Programmer[MAX_MEMBER];//保存开发团队人员
    private int total;//记录开发团队中实际的人数

    public TeamService() {
    }

    /**
     * @Description 获取开发团队中所有成员
     * @return
     */
    public Programmer[] getTeam() {
        Programmer[] team = new Programmer[total];
        for (int i = 0; i < team.length; i++) {
            team[i] = this.team[i];
        }
        return team;
    }

    /**
     * @Description 将指定的员工添加到开发团队中
     * @param e
     */
    public void addMember(Employee e) throws TeamException {
        //成员已满，无法添加
        if (total >= MAX_MEMBER) {
            throw new TeamException("成员已满，无法添加");
        }
        //该成员不是开发人员，无法添加
        if (!(e instanceof Programmer)) {
            throw new TeamException("该成员不是开发人员，无法添加");
        }
        //该员工已在本开发团队中
        if (isExist(e)) {
            throw new TeamException("该员工已在本开发团队中，无法添加");
        }
//        //该员工已是某团队成员
        Programmer p = (Programmer) e;
////        if (p.getStatus().getNAME().equals("BUSY")){}
//        if ("BUSY".equalsIgnoreCase(p.getStatus().getNAME())){
//            throw new TeamException("该员工已是某团队成员，无法添加");
//        }
//        //该员正在休假，无法添加
//        if ("VOCATION".equalsIgnoreCase(p.getStatus().getNAME())){
//            throw new TeamException("该员正在休假，无法添加");
//        }

        switch (p.getStatus()) {
            case BUSY:
                throw new TeamException("该员工已是某团队成员，无法添加");
            case VOCATION:
                throw new TeamException("该员正在休假，无法添加");
        }

        //团队中至多只能有一名架构师
        //团队中至多只能有两名设计师
        //团队中至多只能有三名程序员

        //获取team中已有的成员的类型
        int numOfArch = 0, numOfDes = 0, numOfPro = 0;
        for (int i = 0; i < total; i++) {
            if (team[i] instanceof Architect) {
                numOfArch ++;
            } else if (team[i] instanceof Designer) {
                numOfDes ++;
            } else if (team[i] instanceof Programmer) {
                numOfPro ++;
            }
        }

        if (p instanceof Architect) {
            if (numOfArch >= 1) {
                throw new TeamException("团队中至多只能有一名架构师");
            }
        } else if (p instanceof Designer) {
            if (numOfDes >= 2) {
                throw new TeamException("团队中至多只能有两名设计师");
            }
        } else if (p instanceof Programmer) {
            if (numOfPro >= 3) {
                throw new TeamException("团队中至多只能有三名程序员");
            }
        }

        p.setMemberId(counter++);
        p.setStatus(Status.BUSY);
        //将人员加入到team中
        team[total++] = p;
    }

    /**
     * @Description 判断指定的员工是否已经存在于现有的开发团队中
     * @param e
     * @return
     */
    private boolean isExist(Employee e) {
        for (int i = 0; i < total; i++) {
            if (team[i].getId() == e.getId()) {
                return true;
            }
        }
        return false;
    }

    /**
     * @Description 删除指定的成员
     * @param memberId
     */
    public void removeMember(int memberId) throws TeamException {
        int i = 0;
        for (; i < total; i++) {
            //找到
            if (team[i].getMemberId() == memberId)  {
                team[i].setStatus(Status.FREE);
                break;
            }
        }

        //未找到memberId
        if (i == total) {
            throw new TeamException("找不到指定员工，删除失败");
        }

        //后一个将前一个覆盖
        for (int j = i + 1; j < total; j++) {
            team[j - 1] = team[j];
        }

        team[--total] = null;

    }


}
